#include "bytearr_parser.h"
#include "string.h"
CBytearr_parser::CBytearr_parser()
{
	m_is_big_endian = false;
	m_data_pkt.reserve(512);
}

CBytearr_parser::~CBytearr_parser()
{
}

uint8_t CBytearr_parser::get_byte(int pos)
{
	if (pos >= m_data_pkt.size())
	{
		return 0;
	}
	return m_data_pkt[pos];
}

uint16_t CBytearr_parser::get_uint16(int pos)
{
	if (pos >= m_data_pkt.size())
	{
		return 0;
	}
	U16ToU8Array u16converter;

	u16converter.u8array[0] = (uint16_t)m_data_pkt[pos++];
	u16converter.u8array[1] = (uint16_t)m_data_pkt[pos++];

	return u16converter.u16val;
}

uint32_t CBytearr_parser::get_uint32(int pos)
{
	if (pos >= m_data_pkt.size())
	{
		return 0;
	}
	U32ToU8Array converter;

	converter.u8array[0] = (uint16_t)m_data_pkt[pos++];
	converter.u8array[1] = (uint16_t)m_data_pkt[pos++];
	converter.u8array[2] = (uint16_t)m_data_pkt[pos++];
	converter.u8array[3] = (uint16_t)m_data_pkt[pos++];

	return converter.u32val;
}

uint64_t CBytearr_parser::get_uint64(int pos)
{
	if (pos >= m_data_pkt.size())
	{
		return 0;
	}
	U64ToU8Array converter;
	converter.u8array[0] = (uint16_t)m_data_pkt[pos++];
	converter.u8array[1] = (uint16_t)m_data_pkt[pos++];
	converter.u8array[2] = (uint16_t)m_data_pkt[pos++];
	converter.u8array[3] = (uint16_t)m_data_pkt[pos++];
	converter.u8array[4] = (uint16_t)m_data_pkt[pos++];
	converter.u8array[5] = (uint16_t)m_data_pkt[pos++];
	converter.u8array[6] = (uint16_t)m_data_pkt[pos++];
	converter.u8array[7] = (uint16_t)m_data_pkt[pos++];

	return converter.u64val;
}

uint8_t CBytearr_parser::pull_byte()
{
	if(m_index > m_data_pkt.size() -1)
	{
		return 0;
	}
	int dest_idx = m_index;
	if (dest_idx < m_data_pkt.size())
	{
		m_index++;
	}
	return m_data_pkt[dest_idx];
}

uint16_t CBytearr_parser::pull_uint16( )
{
	U16ToU8Array u16converter;

	u16converter.u8array[0] = (uint16_t)m_data_pkt[m_index++];
	u16converter.u8array[1] = (uint16_t)m_data_pkt[m_index++];

	return u16converter.u16val;
}

uint32_t CBytearr_parser::pull_uint32()
{
	U32ToU8Array converter;
	
	converter.u8array[0] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[1] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[2] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[3] = (uint16_t)m_data_pkt[m_index++];

	return converter.u32val;
}

uint64_t CBytearr_parser::pull_uint64()
{
	U64ToU8Array converter;
	converter.u8array[0] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[1] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[2] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[3] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[4] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[5] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[6] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[7] = (uint16_t)m_data_pkt[m_index++];

	return converter.u64val;
}

uint16_t CBytearr_parser::pull_uint16_be()
{
	U16ToU8Array u16converter;

	u16converter.u8array[1] = (uint16_t)m_data_pkt[m_index++];
	u16converter.u8array[0] = (uint16_t)m_data_pkt[m_index++];

	return u16converter.u16val;
}

uint32_t CBytearr_parser::pull_uint32_be()
{
	U32ToU8Array converter;

	converter.u8array[0] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[1] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[2] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[3] = (uint16_t)m_data_pkt[m_index++];

	return EndianSwap32(converter.u32val);
}


uint64_t CBytearr_parser::pull_uint64_be()
{
	U64ToU8Array converter;

	converter.u8array[0] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[1] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[2] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[3] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[4] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[5] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[6] = (uint16_t)m_data_pkt[m_index++];
	converter.u8array[7] = (uint16_t)m_data_pkt[m_index++];

	return EndianSwap64(converter.u64val);
}

string CBytearr_parser::pull_string()
{
	m_tmp_str = (char*)&m_data_pkt[m_index];

	m_index += (m_tmp_str.length() + 1);

	return m_tmp_str;
}

void CBytearr_parser::print_byte(const char* title)
{
	int i = 0;
	char tmp_buf[8196] = { 0 };
	int len = 0;

	sprintf(tmp_buf, "%s len %d ", title, m_data_pkt.size());
	len = strlen(tmp_buf);
	for (i = 0; i < m_data_pkt.size(); i++)
	{
		sprintf(tmp_buf + len, "%02x ", m_data_pkt[i]);

		len = strlen(tmp_buf);
	}
	sprintf(tmp_buf + len, "\n");
	printf(tmp_buf);
}

int   CBytearr_parser::init_data(char* data, int size)
{
	m_data_pkt.clear();
	int i = 0;
	for (i = 0; i < size; i++)
	{
		m_data_pkt.push_back(data[i]);
	}
	m_index = 0;
	return (int)m_data_pkt.size();
}

int   CBytearr_parser::init_vec(vector<uint8_t>& vec_byte)
{
	m_data_pkt.clear();
	m_data_pkt = vec_byte;
	m_index = 0;
	return (int)m_data_pkt.size();
}